package com.wifi.a007.wifiFragment;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.StrictMode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.wifi.a007.ViewName;
import com.wifi.a007.service.ArmService;
import com.wifi.a007.service.TranslateService;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;



/**
 * Created by guoshuyu on 2017/6/16.
 */

public class CustomActionWebView extends WebView {

    static String TAG = "CustomActionWebView";

    List<String> menuList = new ArrayList<>();

    ActionMode mActionMode;

    List<String> mActionList = new ArrayList<>();

    ActionSelectListener mActionSelectListener;

    public CustomActionWebView(Context context) {
        super(context);
    }

    public CustomActionWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomActionWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    /**
     * 处理item，处理点击
     * @param actionMode
     */
    private ActionMode resolveActionMode(ActionMode actionMode) {
        Log.i("acton","resolveAction");
        if(actionMode != null) {
            final Menu menu = actionMode.getMenu();
            mActionMode = actionMode;
            menu.clear();
            for (int i = 0; i < mActionList.size(); i++) {
                menu.add(mActionList.get(i));
            }
            for (int i = 0; i < menu.size(); i++) {
                MenuItem menuItem = menu.getItem(i);
                menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        getSelectedData((String) item.getTitle());
                        releaseAction();
                        return true;
                    }
                });
            }
        }
        mActionMode = actionMode;
        return actionMode;
    }

    @Override
    public ActionMode startActionMode(ActionMode.Callback callback) {
        ActionMode actionMode = super.startActionMode(callback);
        return resolveActionMode(actionMode);
    }

    @Override
    public ActionMode startActionMode(ActionMode.Callback callback, int type) {
        ActionMode actionMode = super.startActionMode(callback, type);
        return resolveActionMode(actionMode);
    }

    private void releaseAction() {
        if (mActionMode != null) {
            mActionMode.finish();
            mActionMode = null;
        }
    }

    /**
     * 点击的时候，获取网页中选择的文本，回掉到原生中的js接口
     * @param title 传入点击的item文本，一起通过js返回给原生接口
     */
    private void getSelectedData(String title) {

        String js = "(function getSelectedText() {" +
                "var txt;" +
                "var title = \"" + title + "\";" +
                "if (window.getSelection) {" +
                "txt = window.getSelection().toString();" +
                "} else if (window.document.getSelection) {" +
                "txt = window.document.getSelection().toString();" +
                "} else if (window.document.selection) {" +
                "txt = window.document.selection.createRange().text;" +
                "}" +
                "JSInterface.callback(txt,title);" +
                "})()";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            evaluateJavascript("javascript:" + js, null);


        } else {

            loadUrl("javascript:" + js);

        }
    }

    public void linkJSInterface() {
        addJavascriptInterface(new ActionSelectInterface(this), "JSInterface");
    }

    /**
     * 设置弹出action列表
     * @param actionList
     */
    public void setActionList(List<String> actionList) {
        mActionList = actionList;
    }

    /**
     * 设置点击回掉
     * @param actionSelectListener
     */
    public void setActionSelectListener(ActionSelectListener actionSelectListener) {
        this.mActionSelectListener = actionSelectListener;
    }

    /**
     * 隐藏消失Action
     */
    public void dismissAction() {
        releaseAction();
    }


    /**
     * js选中的回掉接口
     */
    private class ActionSelectInterface {

        CustomActionWebView mContext;

        ActionSelectInterface(CustomActionWebView c) {
            mContext = c;
        }

        @JavascriptInterface
        public void callback(final String value, final String title) {
            if(mActionSelectListener != null) {
                mActionSelectListener.onClick(title, value);
            }
        }
    }



  public  void  init() {





      setWebViewClient(new WebViewClient());

      //设置item

      initMenu();
      //链接js注入接口，使能选中返回数据
     linkJSInterface();


      getSettings().setBuiltInZoomControls(true);
      getSettings().setDisplayZoomControls(false);
      //使用javascript
      getSettings().setJavaScriptEnabled(true);
      getSettings().setDomStorageEnabled(true);


      //增加点击回调
      this.setActionSelectListener(new ActionSelectListener() {
          @Override
          public void onClick(String title, String selectText) {


             // Toast.makeText(ViewName.mContext, "点击一次玩玩" , Toast.LENGTH_SHORT).show();


              afterMenuClick( title,  selectText);
          }
      });

      //加载url
      this.postDelayed(new Runnable() {
          @Override
          public void run() {
              loadUrl("http://www.bbc.com/news");
          }
      }, 1000);

  }


    private  void afterMenuClick(String title, String selectText){

        switch (title){
            case  "翻译":

                Intent intent =new Intent(ViewName.mContext, TranslateService.class);
                intent.putExtra("word",selectText);
                ViewName.mContext.startService(intent);

            break;


            case  "粘贴":
                Toast.makeText(ViewName.mContext, "粘贴内容是:"+selectText , Toast.LENGTH_SHORT).show();

           break ;

            case "arm查询":
                Intent intent1 =new Intent(ViewName.mContext, ArmService.class);
                intent1.putExtra("word",selectText);
                ViewName.mContext.startService(intent1);

                break;

            default:




        }



    }

    private void  initMenu (){
        menuList.add("arm查询");
        menuList.add("翻译");
        menuList.add("粘贴");
        menuList.add("其他");

        setActionList(menuList);

    }



}